/*parameters linked to investigator.jsp*/
var siteId = "#siteId";
var siteSelected = "#searchSite";
var showMoreInfoId = "#sites-show-hide";
var siteMoreInfo = "#selected-site-Information";
var cdaId = "#cda-show-hide";
var cdaInfo = "#selected-cda-Information";

/*Site constructor*/
function Site(id, corporateName, institucion,ultimaActualizacionDatos){
    var site = new Object();
    site.id = id;
    site.corporateName = corporateName;
    site.institucion = institucion;
    site.ultimaActualizacionDatos = ultimaActualizacionDatos;
    return site;
}

function searchSites(name, url){
    windowCaller("Select site", url, { "site.institucion": name });
}

$(document).ready(function() {
    $('#labelRequiredSite').hide();
    $('#labelRequiredPI').hide();
});


/*
 * Receives an investigator
 * */
function closeWindows(site){
    $.window.closeAll();
    $(siteSelected).val(site.institucion);
    $(siteId).val(site.id);

    /*
     Build html more information for site. It has information about its addresses
     */
    builMoreInfoHTMLForSites(site);

}

/*It prints addresses information*/
function builMoreInfoHTMLForSites(site){
    $(siteMoreInfo).empty();
    $(cdaInfo).empty();

    $.ajax({
        type: "POST",
        url: "getSiteAddressesById",
        data: {"id" :site.id},
        success: function(result)
        {
            $(siteMoreInfo).append(result);
            $(showMoreInfoId).show();

        }
    });


    $.ajax({
        type: "POST",
        url: "getCdaById",
        data: {"id" :site.id, "investigatorid": $('#piId').val() },
        success: function(result)
        {
            $(cdaInfo).empty();
            if(result.trim() != ''){
                $(cdaInfo).append(result);
                $(cdaId).show();
            }else{
                $(cdaInfo).append("<span style=\"font-size:1.2em;\">No results found</span>");
                $(cdaId).show();
            }
        }
    });

}

function validate(){
    var validSite;

    if($('#siteId') != null && $('#siteId').val() == ""){
        $('#searchSite').css("border-color", "red");
        $('#labelRequiredSite').show();
        validSite = false;
    }
    else{
        $('#labelRequiredSite').hide();
        $('#searchSite').css("border", "2px inset rgb(0, 0, 0)");
        validSite = true;
    }

    var validPI;

    if($('#piId') != null && $('#piId').val() == ""){
        $('#searchPI').css("border-color", "red");
        $('#labelRequiredPI').show();
        validPI = false;
    }
    else{
        $('#labelRequiredPI').hide();
        $('#searchPI').css("border", "2px inset rgb(0, 0, 0)");
        validPI = true;
    }

    return (validSite && validPI);

}